package com.xiao.mall.message.rabbitmq.rest;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.xiao.mall.foundation.resource.AbstractResource;
import com.xiao.mall.foundation.web.util.PaginationUtil;
import com.xiao.mall.message.service.dto.UserNotificationDto;
import com.xiao.mall.message.domain.Notification;
import com.xiao.mall.message.service.NotificationService;
import com.xiao.mall.message.service.criteria.NotificationCriteria;
import com.xiao.mall.message.service.dto.NotificationDto;
import com.xiao.mall.message.service.dto.UserLoginDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/notification")
@Slf4j
@Api(tags = "公告,提醒,信息", value = "消息中心-消息对象")
public class NotificationResource extends AbstractResource<Notification, NotificationDto, Long> {

    private final NotificationService notificationService;
    public NotificationResource(NotificationService service) {
        super(service, Notification.class.getSimpleName());
        this.notificationService = service;
    }

    @ApiOperation(value = "新建")
    @PostMapping
    public ResponseEntity<NotificationDto> save(@RequestBody NotificationDto dto) {
        return ResponseEntity.ok(notificationService.create(dto));
    }

    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    @DeleteMapping("{id}")
    public ResponseEntity<Void> deleteById(@PathVariable(name = "id") Long id) {
        return deleteEntity(id);
    }


    @ApiOperation(value = "更新角色", notes = "更新角色")
    @PutMapping("{id}")
    public ResponseEntity<NotificationDto> update(
            @PathVariable("id") Long id,
            @Validated @RequestBody NotificationDto dto) {
        return ResponseEntity.ok(notificationService.update(id, dto));
    }

    @GetMapping("{id}")
    @ApiOperation("根据ID查询")
    public ResponseEntity<NotificationDto> findById(@PathVariable("id") Long id) {
        return findEntityById(id);
    }

//    @GetMapping
//    @ApiOperation("查询所有数据")
//    public ResponseEntity<List<NotificationDto>> findAll(NotificationCriteria criteria, Pageable pageable) throws InterruptedException {
//        return findAllByPage(criteria, pageable);
//    }

    @GetMapping("all")
    @ApiOperation("查询所有数据")
    public ResponseEntity<List<NotificationDto>> findAll() {
        final List<NotificationDto> UserDtoList = notificationService.findAll();
        HttpHeaders headers = PaginationUtil.generateListHttpHeaders(UserDtoList);
        return ResponseEntity.ok().headers(headers).body(UserDtoList);
    }

    @Override
    protected Long getId(NotificationDto dto) {
        return dto.getId();
    }

//    @ApiOperation("新增消息内容")
//    @PostMapping("")
//    public ResponseEntity<MessageDto> createMessage(@RequestBody MessageDto messageDto) throws URISyntaxException {
//        return createEntity(messageDto);
//    }

    @ApiOperation("新增待发送用户消息")
    @PostMapping("/toSending")
    public ResponseEntity<UserNotificationDto> createChannelNotification(@RequestParam(name = "channel") String channel,
                                                                         @RequestParam(name = "to") String userId,
                                                                         @RequestBody NotificationDto dto) {
        UserNotificationDto userNotificationDto = notificationService.createChannelNotificationToUser(userId, dto);
        return ResponseEntity.ok().body(userNotificationDto);
    }

    @ApiOperation("用户登录绑定参数")
    @PostMapping("/user/binding")
    public ResponseEntity<Void> userLoginBinding(@RequestBody UserLoginDto userLoginDto) throws JsonProcessingException {
        notificationService.userLoginBinding(userLoginDto);
        return ResponseEntity.noContent().build();
    }

    @ApiOperation("消息分页查询")
    @GetMapping("")
    public ResponseEntity<List<NotificationDto>> findNotificationPage(NotificationCriteria notificationCriteria, Pageable pageable) throws InterruptedException {
        return findAllByPage(notificationCriteria, pageable);
    }
}
